package com.happyclub.lbj.service.impl;

import com.happyclub.lbj.service.IActivityCommont;
import com.happyclub.lbjdao.ActivityCommont;
import com.happyclub.lbjdao.ActivityCommontReply;
import com.happyclub.paul.dao.UserRepostitory;
import com.happyclub.pojo.TbActivityCommont;
import com.happyclub.pojo.TbUser;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

import static com.happyclub.errormsg.ErrorMsgProvider.SYSTEM_BUSY;

/**
 * @author 高欢
 * @description TODO
 * @date 2019/4/8
 */
@Service
public class ActivityCommontImpl implements IActivityCommont {
    @Autowired
    ActivityCommont IactivityCommont;

    @Autowired
    ActivityCommontReply iActivityCommontReply;

    @Autowired
    UserRepostitory userRepostitory;

    @Transactional
    @Override
    public JsonResult saveActivityCommont(TbActivityCommont activityCommont) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        TbActivityCommont save = IactivityCommont.save(activityCommont);
        if (save==null){
            jsonResult.setErrorCode("500");
            jsonResult.setMsg(SYSTEM_BUSY);
            return jsonResult;
        }
        jsonResult.setResult(save);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult delActivityCommont(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        IactivityCommont.deleteById(id);
        iActivityCommontReply.deleteTbActivityCommontRepliesByCommontId(id);
        return jsonResult;
    }

    @Override
    public JsonResult findAllActivityCommontById(String activityId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbActivityCommont> orderByCreateTime = IactivityCommont.findActivityById(activityId);
        if (orderByCreateTime.size()==0){
            jsonResult.setMsg("目前该活动还没有评论,赶快来抢沙发吧...");
            jsonResult.setErrorCode("500");
            return jsonResult;
        }
        for (TbActivityCommont tc:orderByCreateTime){
            TbUser tbUser = userRepostitory.queryUserById(tc.getUserId());
            tc.setNickName(tbUser.getNickName());
        }
        jsonResult.setResult(orderByCreateTime);
        return jsonResult;
    }
}
